<template>
	<view v-if="goods_info.goods_name">
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(v,i) in goods_info.pics" :key="i">
				<image :src="v.pics_big || defalutPic " @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		<view class="goods-info-box">
			<view class="price">
				￥{{goods_info.goods_price}}
			</view>
			<view class="goods-info-body">
				<view class="goods-name">
					{{goods_info.goods_name}}
				</view>
				<view class="favi">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yf">
				快递,免运费
			</view>
		</view>
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		<view class="goods-nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				defalutPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-meium.png',
				goods_info: {},
				allCount: 0,
				options: [{
						text: '店铺',
						icon: 'shop'
					},
					{
						icon: 'cart',
						text: '购物车',
						info: 0
					}
				],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		computed: {
			// ...mapState(['num']),
			// ...mapState('m_cart', ['cart']),
			...mapState({
				cart: (state) => state.m_cart.cart
			}),
			...mapGetters('m_cart', ['total'])
		},

		onLoad(options) {
			this.getGoodsDetail(options.goods_id);
		},
		watch: {
			total: {
				handler(newVal) {
					const findResult = this.options.find(v => v.text === '购物车');
					if (findResult) {
						findResult.info = newVal
					}
				},
				immediate: true
			}
		},
		methods: {
			...mapMutations('m_cart', ['addToCart']),
			getGoodsDetail(goods_id) {
				uni.request({
					url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/detail',
					method: "GET",
					data: {
						goods_id
					}
				}).then(res => {
					// console.log(res.data.message)
					res.data.message.goods_introduce = res.data.message.goods_introduce.replace(/<img /g,
						'<img style="display:block;" ').replace(/webp/, 'jpg')
					this.goods_info = res.data.message;
				})
			},
			preview(i) {
				uni.previewImage({
					urls: this.goods_info.pics.map(x => x.pics_big),
					current: i
				})
			},
			onClick(e) {
				// console.log(e)
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart',
					})
				}
			},
			buttonClick(e) {
				// console.log(e)
				if (e.content.text === '加入购物车') {
					const goods = {
						goods_id: this.goods_info.goods_id, //ID
						goods_name: this.goods_info.goods_name, //商品名
						goods_price: this.goods_info.goods_price, //商品价格
						goods_count: 1, //初始数量为1
						goods_small_logo: this.goods_info.goods_small_logo, //商品图片
						goods_state: true //商品的勾选状态
					}
					this.addToCart(goods)

				}
				// console.log(this.cart)
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;

		swiper-item {
			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.goods-info-box {
		padding: 20rpx;
		padding-right: 0;
		margin: 20rpx 0;

		.price {
			font-size: 40rpx;
			color: #c00000;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.goods-name {
				margin-right: 20rpx;
				font-size: 26rpx;
			}

			.favi {
				width: 240rpx;
				font-size: 24rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				border-left: 2rpx solid #efefef;
				color: gray;
			}

		}

		.yf {
			font-size: 24rpx;
			color: gray;
			margin: 20rpx 0;
		}
	}

	.goods-nav {
		position: sticky;
		bottom: 0;
	}
</style>
